(function($) {

// popupBox 弹出lightbox层
$.fn.popupBox = function(options) {
    var defaults = {
            wrap : $('body'),
            layerPopup : $('.layer-popup'),
            action : 'open'
        };
    options = $.extend({}, defaults, options);


    if (options.action == 'open') {
        var target = $(this).clone();
        var orgPosition = $(this).parent();
        openBox();
    } else if (options.action == 'close') {
        closeBox();
    }

    function openBox() {
        options.wrap.addClass('layer-wrap-backstage');
        target.appendTo(options.layerPopup);
        var winwidth = $(window).width();
        var width = options.layerPopup.children().width();
        if (winwidth<width) {width=winwidth-50;};
        options.layerPopup.children().append('<div class="popup-close" title="关闭"></div>');
        options.layerPopup.find('.popup-close').css({'margin-left':width});
        options.layerPopup.show();
        options.layerPopup.children().show();

        target.click(function(event) {
            console.log(1);
            event.stopPropagation();
        });

        options.layerPopup.find('.popup-close').click(closeBox);
        options.layerPopup.click(closeBox);
    }

    function closeBox() {
        options.wrap.removeClass('layer-wrap-backstage');
        options.layerPopup.hide().children().remove();
    }


};


// 解析时间戳
$.fn.parseStamp = function() {
    var c = parseInt($(this).attr("data-stamp"));
    $(this).html(function(d) {
        function c(a) {
            10 > a && (a = "0" + a);
            return a;
        }
        if (!d) return "\u7334\u5e74\u9a6c\u6708";
        var e = new Date,
        a = Date.parse(e),
        b = 6E4 * e.getTimezoneOffset(),
        e = Date.parse(e.getFullYear()) + b,
        b = a - a % 864E5 + b,
        f = b - 864E5,
        a = a - d;
        if (0 > a) return "\u6765\u81ea\u672a\u6765";
        if (0 <= a && 6E4 > a) return a / 1E3 + "\u79d2\u524d";
        if (6E4 <= a && 36E5 > a) return Math.floor(a / 6E4) + "\u5206\u949f\u524d";
        var a = new Date(d),
        g = c(a.getHours()),
        h = c(a.getMinutes());
        if (b <= d) return "\u4eca\u5929 " + g + ":" + h;
        if (f <= d) return "\u6628\u5929 " + g + ":" + h;
        b = a.getMonth() + 1;
        f = a.getDate();
        return e <= d ? b + "\u6708" + f + "\u65e5 " + g + ":" + h: a.getFullYear() + "-" + b + "-" + f + " " + g + ":" + h;
    } (c));
    return $(this);
};

$.fn.parseStampPre=function(){var c=$(this).attr("data-time");$(this).attr("data-stamp",Date.parse(new Date(c)));return $(this);};

})(jQuery);



// 提取clone层内的模版
function getCloneLib(options) {
    var lib =[];
    var node = $('.layer-clone').children('.node-clone');
    var sum = node.index()+1;

    for (var i = 0; i < sum; i++) {
        var html = node.eq(i).html();
        var title = node.eq(i).attr('data-title');
        lib[i] = html;
        lib[title] = i;
    };

    $('.layer-clone').remove();

    return lib;
}

// 调用模版
function searchCloneLib(title) {
    return $(cloneLib[cloneLib[title]]);
}

